package com.zhoug.tmap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhoug.logging.Logger;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.infowindow.InfoWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * marker管理工具
 * @Author: zhoug
 * @Date: 2024-12-30
 * @Description:
 */
public class MarkerManager {
    private static final String TAG = ">>>MarkerManager";
    private static final boolean DEBUG = false;


    /**
     * 移除原来的marker 添加新的全部marker
     */
    public static final int DELETE = 0;
    /**
     * 保留原有的marker 添加新的全部marker
     */
    public static final int KEEP = 1;

    /**
     * 删除原来多余的 保存相交的
     */
    public static final int DELETE_KEEP = 2;
    /**
     * 保存原来多余的 替换相交的
     */
    public static final int KEEP_REPLACE = 3;
    /**
     * 保存原来多余的 保存相交的
     */
    public static final int KEEP_KEEP = 4;


    private MapView mMapView;
    /**
     * 已经添加到地图上的marker
     */
    private final List<Marker> mAddMarkers=new ArrayList<>();
    /**
     * 全部marker,包括已经添加到地图上的和未添加到地图上的
     */
    private final List<Marker> mAllMarkers=new ArrayList<>();

    public MarkerManager(MapView mapView) {
        this.mMapView = mapView;

    }





    public void addMarker( @NonNull List<Marker> markers, int type) {
        Logger.d(TAG, "markers.size=" + markers.size());
        List<Marker> olds =mAddMarkers;
        if ( olds.isEmpty()) {
            //没有原来的,直接添加
            if (!markers.isEmpty()) {
                addMarkers(olds, markers);
            }
            return;
        }
        if (markers.isEmpty()) {
            //没有新增的
            if (type == DELETE || type == DELETE_KEEP) {
                removeMarkers(olds, olds);
            }
            return;
        }
        //############
        if (type == DELETE) {
            removeMarkers(olds, olds);
            addMarkers(olds, markers);
            return;
        }
        if (type == KEEP) {
            addMarkers(olds, markers);
            return;
        }
        List<Marker> sameNew = Utils.findSame(olds, markers);
        if (sameNew == null || sameNew.isEmpty()) {
            //没有交集
            if (type == DELETE_KEEP) {
                removeMarkers(olds, olds);
            }
            addMarkers(olds, markers);
            return;
        }
        //有交集
        // DELETE_KEEP KEEP_REPLACE KEEP_KEEP
        List<Marker> outOld = new ArrayList<>(olds);
        outOld.removeAll(sameNew);
        List<Marker> outNew = new ArrayList<>(markers);
        outNew.removeAll(sameNew);
        if (type == DELETE_KEEP) {
            removeMarkers(olds, outOld);
            addMarkers(olds, outNew);
            return;
        }
        if (type == KEEP_REPLACE) {
            List<Marker> sameOld = new ArrayList<>(olds);
            if (!outOld.isEmpty()) {
                sameOld.removeAll(outOld);
            }
            removeMarkers(olds, sameOld);
            addMarkers(olds, markers);
            return;
        }
        if (type == KEEP_KEEP) {
            addMarkers(olds, outNew);
        }
    }


    private void addMarkers(List<Marker> olds, List<Marker> mks) {
        if (!mks.isEmpty()) {
            mMapView.getOverlays().addAll(mks);
            olds.addAll(mks);
            if (DEBUG) {
                Logger.d(TAG, "addMarkers:" + mks.size());
            }
        }
    }

    private void removeMarkers(List<Marker> olds, List<Marker> mks) {
        if (!mks.isEmpty()) {
            if (DEBUG) {
                Logger.d(TAG, "removeMarkers:start "+mks.size());
            }
            removeMarkers(mks);
            if (olds == mks) {
                olds.clear();
            } else {
                olds.removeAll(mks);
            }
            if (DEBUG) {
                Logger.d(TAG, "removeMarkers: end");
            }
        }
    }


    /**
     * 移除添加到地图上的marker
     *
     */
    public void removeMarkers() {
        removeMarkers(mAddMarkers);
        mAddMarkers.clear();
    }

    public void removeMarker(Marker marker){
        if(marker!=null){
            //移除InfoWindow
            InfoWindow infoWindow = marker.getInfoWindow();
            if(infoWindow!=null && infoWindow.isOpen()){
                infoWindow.close();
            }
            mMapView.getOverlays().remove(marker);
        }
    }

    public  void removeMarkers(List<Marker> markers){
        if(markers!=null && !markers.isEmpty()){
            for (Marker marker : markers) {
                removeMarker(marker);
            }
        }
    }

    /**
     * 获取添加到地图上的marker
     *
     * @return
     */
    public @Nullable List<Marker> getMarkers( ) {
        return mAddMarkers;
    }


}
